<template>
  <div class="markdown-content" v-html="renderedContent"></div>
</template>

<script>
import { computed, defineComponent } from 'vue'
import { marked } from 'marked'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'

export default defineComponent({
  name: 'MarkdownRenderer',
  props: {
    content: {
      type: String,
      required: true,
    },
  },
  setup(props) {
    // 配置marked选项
    marked.use({
      breaks: true,
      gfm: true,
      pedantic: false,
    })

    // 渲染Markdown内容
    const renderedContent = computed(() => {
      try {
        return marked(props.content || '')
      } catch (err) {
        console.error('Markdown rendering error:', err)
        return props.content || ''
      }
    })

    return {
      renderedContent,
    }
  },
})
</script>

<style scoped>
.markdown-content {
  font-size: 14px;
  line-height: 1.6;
  color: inherit;
}

.markdown-content :deep(h1),
.markdown-content :deep(h2),
.markdown-content :deep(h3),
.markdown-content :deep(h4),
.markdown-content :deep(h5),
.markdown-content :deep(h6) {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-content :deep(h1) {
  font-size: 2em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid #eaecef;
}

.markdown-content :deep(h2) {
  font-size: 1.5em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid #eaecef;
}

.markdown-content :deep(h3) {
  font-size: 1.25em;
}

.markdown-content :deep(p) {
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-content :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1em 0;
  border-radius: 8px;
}

.markdown-content :deep(code) {
  padding: 0.2em 0.4em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(27, 31, 35, 0.05);
  border-radius: 3px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
}

.markdown-content :deep(pre) {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f6f8fa;
  border-radius: 3px;
  margin-bottom: 16px;
}

.markdown-content :deep(pre code) {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

.markdown-content :deep(blockquote) {
  margin: 0;
  padding: 0 1em;
  color: #6a737d;
  border-left: 0.25em solid #dfe2e5;
}

.markdown-content :deep(ul),
.markdown-content :deep(ol) {
  padding-left: 2em;
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-content :deep(table) {
  display: block;
  width: 100%;
  overflow: auto;
  margin-top: 0;
  margin-bottom: 16px;
  border-spacing: 0;
  border-collapse: collapse;
}

.markdown-content :deep(table th),
.markdown-content :deep(table td) {
  padding: 6px 13px;
  border: 1px solid #dfe2e5;
}

.markdown-content :deep(table tr) {
  background-color: #fff;
  border-top: 1px solid #c6cbd1;
}

.markdown-content :deep(table tr:nth-child(2n)) {
  background-color: #f6f8fa;
}

.markdown-content :deep(hr) {
  height: 0.25em;
  padding: 0;
  margin: 24px 0;
  background-color: #e1e4e8;
  border: 0;
}

/* 添加暗色主题支持 */
@media (prefers-color-scheme: dark) {
  .markdown-content {
    color: #c9d1d9;
  }

  .markdown-content :deep(h1),
  .markdown-content :deep(h2) {
    border-bottom-color: #30363d;
  }

  .markdown-content :deep(code) {
    background-color: rgba(240, 246, 252, 0.15);
  }

  .markdown-content :deep(pre) {
    background-color: #161b22;
  }

  .markdown-content :deep(blockquote) {
    color: #8b949e;
    border-left-color: #30363d;
  }

  .markdown-content :deep(table th),
  .markdown-content :deep(table td) {
    border-color: #30363d;
  }

  .markdown-content :deep(table tr) {
    background-color: #0d1117;
    border-top-color: #30363d;
  }

  .markdown-content :deep(table tr:nth-child(2n)) {
    background-color: #161b22;
  }

  .markdown-content :deep(hr) {
    background-color: #30363d;
  }
}
</style> 